#pragma once
// #define _GNU_SOURCE  
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include "Buffer.h"
#include <stdbool.h>
#include <sys/stat.h>
#include "HttpResponse.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include<errno.h>
#include <ctype.h>
#include <sys/sendfile.h>
#include"Log.h"
#include<string.h>
struct RequestHeader
{
    char *key;
    char *value;
};

enum HttpRequestStates
{
    ParseReqLine,
    ParesReqHeaders,
    ParseReqBody,
    ParseReqDone
};
 struct HttpRequest
{
    char *method;
    char *url;
    char *version;
    // 请求体有多个键值对
    struct RequestHeader *requestHeaders;
    int requestHeadersNum;
    enum HttpRequestStates curState;
};
// 初始化
struct HttpRequest *httpRequestInit();
// 重置
void httpRequestReset(struct HttpRequest *httpRequest);

void httpRequestResetEx(struct HttpRequest *httpRequest);

void httpRequusetDestory(struct HttpRequest *httpRequest);
char* splitRequestLine(const char* start, const char* end, const char* sub, char** ptr);
// 获取处理状态
enum HttpRequestStates getHttpRequestStates(struct HttpRequest *httpRequest);

// 写一个往HttpRequestStates 添加的函数
void httpRequestAddHeader(struct HttpRequest *httpRequest, const char *key, const char *value);
// 通过key 获取value
char* httpRequestGetHeader(struct HttpRequest *httpRequest, const char *key);



// 解析请求行 ParseReqLine
bool parseHttpRequestLine(struct HttpRequest *httpRequest, struct Buffer *readBuffer);
// 解析请求头
bool parseHttpRequestHeader(struct HttpRequest *httpRequest, struct Buffer *readBuffer);

// 解析http协议
bool parseHttpRequest(struct HttpRequest *httpRequest, struct Buffer *readBuffer, struct HttpResponse *httpResponse, struct Buffer *sendBuffer, int socket);

// 处理http协议
bool processHttpRequest(struct HttpRequest *httpRequest, struct HttpResponse *httpResponse);

// 解码字符串
void decodeMsg(char *to, char *from);

// 获取后缀类型
const char *getFileType(const char *filename);
// 发送文件
void sendFile(const char *filename,struct Buffer* sendbuffer, int cfd);
// 发送目录
void sendDir(const char *dir,struct Buffer* sendbuffer, int cfd);